/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.api;

import com.jeesite.modules.storage.service.MemberService;
import com.jeesite.modules.storage.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 手机号Controller
 */
@RestController(value = "apiphone")
@RequestMapping(value = "/api/phone")
public class PhoneController extends APIController {

    @Autowired
    private MemberService memberService;

    /**
     * 手机号注册
     * @param platform 平台【APP或H5】
     * @param phone 手机号
     * @param code 验证码
     * @return 注册结果
     * 200 注册成功
     * 500 其它
     * 501 未发送验证码
     * 502 验证码错误
     * 503 验证码已过期
     * 504 已注册，未绑定微信
     * 505 已注册，直接登录
     */
    @RequestMapping(method = RequestMethod.POST,value = "register")
    public ServerResponse<String> register(@RequestParam(value="platform") String platform, @RequestParam(value="phone") String phone, @RequestParam(value="code") String code)
    {
        /**
         * 手机注册
         * 500 其它
         * 501 未发送验证码
         * 502 验证码错误
         * 503 验证码已过期
         * 504 已注册，未绑定微信
         * 505 已注册，直接登录
         */
        switch (this.memberService.registerPhone(platform, phone,code)){
            case 200:
            {
                return ServerResponse.createBySuccess(200,"注册成功，去绑定微信",memberService.getIdByPhoneOrOpenId(platform,phone,null));
            }
            case 501:
            {
                return ServerResponse.createBySuccess(501,"未发送验证码");
            }
            case 502:
            {
                return ServerResponse.createBySuccess(503,"验证码错误");
            }
            case 503:
            {
                return ServerResponse.createBySuccess(502,"验证码已过期");
            }
            case 504:
            {
                return  ServerResponse.createBySuccess(504,"已注册，未绑定微信",memberService.getIdByPhoneOrOpenId(platform,phone,null));
            }
            case 505:
            {
                return ServerResponse.createBySuccess(505,"已注册，直接登录");
            }
            default:
            {
               return ServerResponse.createBySuccess(500,"其它");
            }
        }
    }

    /**
     * 手机号登录
     * @param platform 平台【APP或H5】
     * @param phone 手机号
     * @param code 验证码
     * @return 登录结果
     * 200 成功登陆
     * 500 其它
     * 501 未发送验证码
     * 502 验证码错误
     * 503 验证码已过期
     * 504 未注册
     * 505 未绑定微信
     */
    @RequestMapping(method = RequestMethod.POST,value = "login")
    public ServerResponse<String> login(@RequestParam(value="platform") String platform, @RequestParam(value="phone") String phone, @RequestParam(value="code") String code)
    {
        /**
         * 手机登录
         * 500 其它
         * 501 未发送验证码
         * 502 验证码错误
         * 503 验证码已过期
         * 504 未注册
         * 505 未绑定微信
         */
        switch (this.memberService.loginPhone(platform,phone,code)){
            case 200:
            {
                return ServerResponse.createBySuccess(200,"成功登陆",memberService.getIdByPhoneOrOpenId(platform,phone,null));
            }
            case 501:
            {
                return ServerResponse.createBySuccess(501,"未发送验证码");
            }
            case 502:
            {
                return ServerResponse.createBySuccess(503,"验证码错误");
            }
            case 503:
            {
                return ServerResponse.createBySuccess(502,"验证码已过期");
            }
            case 504:
            {
                return  ServerResponse.createBySuccess(504,"未注册");
            }
            case 505:
            {
                return  ServerResponse.createBySuccess(505,"未绑定微信,现在就去绑定",memberService.getIdByPhoneOrOpenId(platform,phone,null));
            }
            default:
            {
                return ServerResponse.createBySuccess(500,"其它");
            }
        }
    }

    /**
     * 绑定手机
     * @param platform 平台【APP或H5】
     * @param unionId 微信UnionId
     * @param openId 微信OpenId
     * @param avatar 微信头像
     * @param nickName 微信昵称
     * @param phone 手机号
     * @param code 验证码
     * @return 绑定结果
     * 200 绑定成功
     * 500 其它
     * 501 未发送验证码
     * 502 验证码错误
     * 503 验证码已过期
     * 504 该手机已绑定其它微信
     */
    @RequestMapping(method = RequestMethod.POST,value = "bind")
    public ServerResponse<String> bind(@RequestParam(value="platform") String platform,@RequestParam(value="phone") String phone, @RequestParam(value="code") String code , @RequestParam(value="unionId") String unionId, @RequestParam(value="openId") String openId,  @RequestParam(value="avatar") String avatar, @RequestParam(value="nickName") String nickName)
    {
        /**
         * 绑定手机
         * 500 其它
         * 501 未发送验证码
         * 502 验证码错误
         * 503 验证码已过期
         * 504 该手机已绑定其它微信
         */
        switch (this.memberService.bindPhone(platform, phone , code , unionId , openId,avatar,nickName)){
            case 200:
            {
                return ServerResponse.createBySuccess(200,"成功绑定",memberService.getIdByPhoneOrOpenId(platform,phone,null));
            }
            case 501:
            {
                return ServerResponse.createBySuccess(501,"未发送验证码");
            }
            case 502:
            {
                return ServerResponse.createBySuccess(502,"验证码错误");
            }
            case 503:
            {
                return ServerResponse.createBySuccess(503,"验证码已过期");
            }
            case 504:
            {
                return  ServerResponse.createBySuccess(507,"该手机号已绑定其它账号");
            }
            default:
            {
                return ServerResponse.createBySuccess(500,"其它");
            }
        }
    }
}
